package cn.initcap.concurrency.concurrency.atomic;

import cn.initcap.concurrency.annoations.ThreadSafe;
import cn.initcap.concurrency.concurrency.ThreadPoolCreatHelper;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;

/**
 * @author initcap
 * @date Created in 2018/6/13 PM7:41.
 */
@ThreadSafe
@Slf4j
public class AtomicIntegerExample {

    /**
     * 总请求数
     */
    private static final int CLIENT_TOTAL = 5000;
    /**
     * 同时并发执行的线程数
     */
    private static final int THREAD_TOTAL = 200;

    public static AtomicInteger count = new AtomicInteger(0);

    public static void add() {
        //这里使用了Unsafe.compareAndSwapInt来判断当前值与底层的值是否相同，相同才会进行+1。
        count.incrementAndGet();
    }

    public static void main(String[] args) throws Exception {
        ThreadPoolCreatHelper.threadPoolRun(CLIENT_TOTAL, THREAD_TOTAL,
                AtomicIntegerExample.class, "add",
                "AtomicIntegerExample");
        log.info("count:{}", count);
    }

}
